package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.math.BigDecimal;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.come.Frame.ZhuFrame;
import org.come.bean.Bbuy;
import org.come.bean.GiveGoodsBean;
import org.come.entity.Goodstable;
import org.come.mouslisten.GiveGoodsMouslisten;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.FormsManagement;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.GsonUtil;
import org.come.until.UserMessUntil;

import com.tool.Document.InputNum;
import com.tool.Document.NumberDocument;
import com.tool.ModerateTask.Hero;
import com.tool.ModerateTask.Task;
import com.tool.ModerateTask.TaskMixDeal;
import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.RoleOperationPanelBtn;
import com.tool.btn.goodbtn;
import com.tool.image.ImageMixDeal;
import com.tool.image.ManimgAttribute;
import com.tool.role.RoleData;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;

/**
 * 给予主面板
 * @author Administrator
 *
 */
public class GiveJpanel extends JPanel implements InputNum{

	private goodbtn[] btnrights;// 放置6个背包切换按钮
	private static JTextField textNumber,// 数量
			textMoney;// 银两

	private JLabel labplayer,// 玩家
			labplyername;// 要给予的玩家姓名

	private RoleOperationPanelBtn btnGive;// 给予按钮
	private JLabel[] GoodsListLabel;// 用来存放24个物品表格

	private Goodstable giveGood;// 选中的物品
	private ManimgAttribute attribute;//给予对象
	public GiveJpanel() throws Exception {
		if (MyIsif.getStyle().equals("水墨UI")){
			this.setPreferredSize(new Dimension(393, 375));
			this.setLayout(null);
			this.setBackground(UIUtils.Color_BACK);
			// 关闭按钮事件
			FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/button/1.png", 1, 12);
			offBtn.setBounds(393 - 37, 10, 25, 25);
			this.add(offBtn);
			GoodsListLabel = new JLabel[24];
			for (int i = 0; i < 24; i++) {
				GoodsListLabel[i] = new JLabel();
				GoodsListLabel[i].setBounds(46 + i%6 * 51, 48 + i/6 * 51, 50, 50);
				GoodsListLabel[i].addMouseListener(new GiveGoodsMouslisten(i, this));
				this.add(GoodsListLabel[i]);
			}
			btnrights = new goodbtn[6];
			for (int i = 0; i < btnrights.length; i++) {
				btnrights[i] = new goodbtn("skin/8100226/newbutton/C0"+(i+1)+".png", 0, this, i);
				btnrights[i].setBounds(353, 54 + i * 35, 39, 31);
				this.add(btnrights[i]);
			}
			// 数量
			textNumber = new JTextField();
			textNumber.setBounds(102, 267, 151, 16);
			textNumber.setText("0");
			textNumber.setForeground(Color.white);
			textNumber.setBackground(UIUtils.Color_BACK);
			textNumber.setBorder(BorderFactory.createEmptyBorder());
			textNumber.setFont(UIUtils.TEXT_FONT1);
			textNumber.setDocument(new NumberDocument(textNumber, 1<<2,this));
			this.add(textNumber);
			// 银两
			textMoney = new JTextField();
			textMoney.setBounds(102, 295, 151, 16);
			textMoney.setText("0");
			textMoney.setForeground(Color.white);
			textMoney.setBackground(UIUtils.Color_BACK);
			textMoney.setBorder(BorderFactory.createEmptyBorder());
			textMoney.setDocument(new NumberDocument(textMoney, 1<<2));
			textMoney.setFont(UIUtils.TEXT_FONT1);
			this.add(textMoney);
			// 玩家
			labplayer = new JLabel();
			labplayer.setText("玩家:");
			labplayer.setBounds(36, 339, 40, 20);
			labplayer.setForeground(Color.black);
			labplayer.setFont(UIUtils.TEXT_FONT1);
			this.add(labplayer);
			// 要给予的玩家姓名
			labplyername = new JLabel();
			labplyername.setBounds(36 + 40, 339, 130, 20);
			labplyername.setForeground(Color.red);
			labplyername.setFont(UIUtils.TEXT_FONT1);
			this.add(labplyername);
			// 给予按钮
			btnGive = new RoleOperationPanelBtn("inkImg/button/32.png", 1, "给予", null,null);
			btnGive.setBounds(270, 335, 60, 26);
			this.add(btnGive);
		}else {
			this.setPreferredSize(new Dimension(363, 385));
			this.setLayout(null);
			this.setBackground(UIUtils.Color_BACK);
			// 关闭按钮事件
			FormsOnOffBtn offBtn = new FormsOnOffBtn("img/xy2uiimg/8_jpg.button.btn_close.jpg", 1, 12);
			offBtn.setBounds(343, 0, 23, 23);
			this.add(offBtn);
			GoodsListLabel = new JLabel[24];
			for (int i = 0; i < 24; i++) {
				GoodsListLabel[i] = new JLabel();
				GoodsListLabel[i].setBounds(19 + i%6 * 51, 45 + i/6 * 51, 50, 50);
				GoodsListLabel[i].addMouseListener(new GiveGoodsMouslisten(i, this));
				this.add(GoodsListLabel[i]);
			}
			// 放置6个背包切换按钮
			btnrights = new goodbtn[6];
			for(int i = 0; i < this.btnrights.length; ++i) {
				this.btnrights[i] = new goodbtn("img/xy2uiimg/44_png.button.tab_pak.png", 0, this, i);
				this.btnrights[i].setBounds(327, 45 + i * 51, 16, 49);
				this.add(this.btnrights[i]);
			}
			// 数量
			textNumber = new JTextField();
			textNumber.setBounds(142, 271, 141, 17);
			textNumber.setText("0");
			textNumber.setForeground(Color.white);
			textNumber.setBackground(UIUtils.Color_BACK);
			textNumber.setBorder(BorderFactory.createEmptyBorder());
			textNumber.setFont(UIUtils.TEXT_FONT1);
			textNumber.setDocument(new NumberDocument(textNumber, 1<<2,this));
			this.add(textNumber);
			// 银两
			textMoney = new JTextField();
			textMoney.setBounds(142, 305, 141, 17);
			textMoney.setText("0");
			textMoney.setForeground(Color.white);
			textMoney.setBackground(UIUtils.Color_BACK);
			textMoney.setBorder(BorderFactory.createEmptyBorder());
			textMoney.setDocument(new NumberDocument(textMoney, 1<<2));
			textMoney.setFont(UIUtils.TEXT_FONT1);
			this.add(textMoney);
			// 玩家
			labplayer = new JLabel();
			labplayer.setText("玩家:");
			labplayer.setBounds(21, 339, 40, 20);
			labplayer.setForeground(Color.red);
			labplayer.setFont(UIUtils.TEXT_FONT1);
			this.add(labplayer);
			// 要给予的玩家姓名
			labplyername = new JLabel();
			labplyername.setBounds(61, 339, 130, 20);
			labplyername.setForeground(Color.red);
			labplyername.setFont(UIUtils.TEXT_FONT1);
			this.add(labplyername);
			// 给予按钮
			btnGive = new RoleOperationPanelBtn("inkImg/hongmu/1_png.button.btn_xy.png", 1, "给 予", null);
			btnGive.setBounds(240, 335, 80, 26);
			this.add(btnGive);
		}
	}

	private ImageIcon icon;
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (MyIsif.getStyle().equals("水墨UI")){
			// 技能
			if (icon == null)
				icon = new ImageIcon("inkImg/background/S140.png");

			g.drawImage(icon.getImage(), 0, 0, 393, 375, this);
			GoodsListFromServerUntil.draw(g, 46, 48);
		}else {
			// 技能
			if (icon == null)
				icon = new ImageIcon("img/xy2uiimg/38_png.xy2uiimg.give_bg.png");

			g.drawImage(icon.getImage(), 0, 0, 363, 385, this);
			GoodsListFromServerUntil.draw(g, 19, 45);
		}
	}

	private int xz = -1;// 当前要购买的物品位置
	/**给予结束清除*/
	public void qingchu() {
		xuanzhong(null, 0);
		textMoney.setText("0");
	}
	/**画出预选边框*/
	public void PaintingText(int shopPlace) {
		GoodsListLabel[shopPlace].setBorder(BorderFactory.createLineBorder(Color.red));
	}
	/**清除预选边框*/
	public void ClearText(int shopPlace) {
		if (xz != shopPlace) {
			GoodsListLabel[shopPlace].setBorder(null);
		}
	}
	/**显示*/
	public void giveShow(ManimgAttribute attribute){
		btnGive.setText("给予");
		this.attribute=null;this.giveGood=null;
		((NumberDocument)textNumber.getDocument()).replace("0");
		((NumberDocument)textMoney.getDocument()).replace("0");
		textNumber.setEditable(true);textMoney.setEditable(true);
		if (attribute.getRoleShow()!=null) {
			labplayer.setText("玩家:");
			labplyername.setText(attribute.getRoleShow().getRolename());
		}else if (attribute.getNpc()!=null) {
			String type=attribute.getNpc().getNpctable().getNpctype();
			if (!(type.equals("71") || type.equals("89") || TaskMixDeal.isgive(attribute.getNpc().getNpctable().getNpcname()))) {
				ImageMixDeal.userimg.Dialogue("他好像不想理你");
				return;
			}
			textMoney.setEditable(false);
			labplayer.setText("NPC:");
			labplyername.setText(attribute.getNpc().getNpctable().getNpcname());
		}else if (attribute.getMapMonsterBean()!=null) {
			textMoney.setEditable(false);
			labplayer.setText("限时收购:");
			labplyername.setText(attribute.getMapMonsterBean().getRobotname());
		}else {
			return;
		}
		this.attribute=attribute;
		FormsManagement.showForm(12);// 显示给予框体
	}
	/**选中*/
	public void xuanzhong(Goodstable goodstable, int shopPlace) {
		if (goodstable==null) {
			if (xz != -1)GoodsListLabel[xz].setBorder(null);
			xz=-1;
			((NumberDocument)textNumber.getDocument()).replace("0");
			giveGood=null;
			if (!(attribute!=null&&attribute.getRoleShow()!=null)) {((NumberDocument)textMoney.getDocument()).replace("0");}
		}else if (giveGood!=null&&giveGood.getRgid().compareTo(goodstable.getRgid())==0) {
			long sum = textNumber.getText().equals("")?0:((NumberDocument)textNumber.getDocument()).getNum().longValue();
			sum=goodstable.getUsetime()>sum?(sum+1):goodstable.getUsetime();
			((NumberDocument)textNumber.getDocument()).replace(sum + "");
			upMoney(sum);
		}else {
			if (xz != -1)GoodsListLabel[xz].setBorder(null);
			xz = shopPlace;
			GoodsListLabel[xz].setBorder(BorderFactory.createLineBorder(Color.red));
			giveGood=goodstable;
			((NumberDocument)textNumber.getDocument()).replace("1");
			upMoney(1);
		}
	}
	/**刷新回收价钱*/
	public void upMoney(long sum){
		if (attribute==null||attribute.getNpc()==null||giveGood==null) {return;}
		String text=null;
		if (attribute.getNpc().getNpctable().getNpctype().equals("71")) {//拾破烂
			Bbuy bbuy = UserMessUntil.getBbuy(giveGood.getGoodsid());
			if (bbuy!=null&&bbuy.getPrice1()!=0) {text=(bbuy.getPrice1()*sum)+"";}
			else {text="0";}
		}else if (attribute.getNpc().getNpctable().getNpctype().equals("89")) {//绑玉回收
			Bbuy bbuy = UserMessUntil.getBbuy(giveGood.getGoodsid());
			if (bbuy!=null&&bbuy.getPrice2()!=0) {text=(bbuy.getPrice2()*sum)+"";}
			else {text="0";}
		}
		if (text!=null) {((NumberDocument)textMoney.getDocument()).replace(text);}
	}
	/** 给予的方法 */
	public void giveMethod() {
		if (ImageMixDeal.userimg.getRoleShow().getBooth_id() != null) {
			ZhuFrame.getZhuJpanel().addPrompt2("摆摊状态不可以给予！");
			return;
		}
		if (attribute == null) {return;}
		int count=0;
		if (giveGood!= null) {
			if (GoodsListFromServerUntil.isExist(giveGood)) {return;}
			if (attribute.getRoleShow()!=null&&GoodsListFromServerUntil.isJY(giveGood)) {ZhuFrame.getZhuJpanel().addPrompt("物品无法给予");return;}
			count=((NumberDocument)textNumber.getDocument()).getNum().intValue();
			if (count<=0) {
				ZhuFrame.getZhuJpanel().addPrompt2("给与的数量不能为0");
				return;
			}
			if (count > giveGood.getUsetime()){
				count=giveGood.getUsetime();
				ZhuFrame.getZhuJpanel().addPrompt2("您给与的物品个数超出你拥有的物品数,将给与的数量调整为你拥有的数量");
			}
		}
		if (attribute.getRoleShow()!=null) {//给与玩家
			givePlayer(count);
			return;
		}
		if (count==0) {
			ZhuFrame.getZhuJpanel().addPrompt2("没有选中的物品");
			return;
		}
		if (count > giveGood.getUsetime())count=giveGood.getUsetime();
		if (attribute.getNpc()!=null) {
			if (TaskMixDeal.isgive(attribute.getNpc().getNpctable().getNpcname())) {
				giveTask(count);
			}else if (attribute.getNpc().getNpctable().getNpctype().equals("71")) {
				giveshipolan(1, count);
			}else if (attribute.getNpc().getNpctable().getNpctype().equals("89")) {
				giveshipolan(2, count);
			}else {
				ZhuFrame.getZhuJpanel().addPrompt2("对你的物品丝毫不感兴趣");
				return;
			}
		}else if (attribute.getMapMonsterBean()!=null) {
			giveshipolan(count);
		}
	}
	/** 给与玩家 */
	public void givePlayer(int count) {
		BigDecimal money=getqian();
		if (money==null) {ZhuFrame.getZhuJpanel().addPrompt2("金钱不足");return;}
		GiveGoodsBean givebean = new GiveGoodsBean();
		givebean.setType(0);
		givebean.setOtherID(attribute.getRoleShow().getRole_id());
		givebean.setOtherName(attribute.getRoleShow().getRolename());
		givebean.setRgid(giveGood!=null?giveGood.getRgid():null);
		givebean.setSum(count);
		givebean.setGold(money);
		String senmes=Agreement.getAgreement().giveAgreement(GsonUtil.getGsonUtil().getgson().toJson(givebean));
		SendMessageUntil.toServer(senmes);//向服务器发送信息
		qingchu();
	}
	/**回收1 金钱 2绑玉回收 */
	public void giveshipolan(int type,int count) {
		//设置给与的时候获取的金钱
		Bbuy bbuy = UserMessUntil.getBbuy(giveGood.getGoodsid());
		if (bbuy == null || (bbuy.getPrice1() == 0 && type == 1) || (bbuy.getPrice2() == 0 && type == 2)) {
			ZhuFrame.getZhuJpanel().addPrompt2("不属于回收范围");
			return;
		}
		GiveGoodsBean givebean = new GiveGoodsBean();
		givebean.setType(type);
		givebean.setRgid(giveGood.getRgid());
		givebean.setSum(count);
		String senmes = Agreement.getAgreement().giveAgreement(GsonUtil.getGsonUtil().getgson().toJson(givebean));
		SendMessageUntil.toServer(senmes);// 向服务器发送信息
		qingchu();
	}
	/**给与任务人物*/
	public void giveTask(int count) {
		Task task=Hero.getHero().PartFinish("给予物品",giveGood.getGoodsname());
		if (task==null) {
			ZhuFrame.getZhuJpanel().addPrompt2("对你的物品丝毫不感兴趣");
			return;
		}
		String mes = Agreement.getAgreement().TaskNAgreement("G"+task.getTaskId()+"|"+giveGood.getRgid()+"|"+count);
		SendMessageUntil.toServer(mes);
		qingchu();
	}
	/**限时回收 */
	public void giveshipolan(int count) {
		GiveGoodsBean givebean = new GiveGoodsBean();
		givebean.setOtherID(new BigDecimal(attribute.getMapMonsterBean().getI()));
		givebean.setType(3);
		givebean.setRgid(giveGood.getRgid());
		givebean.setSum(count);
		String senmes = Agreement.getAgreement().giveAgreement(GsonUtil.getGsonUtil().getgson().toJson(givebean));
		SendMessageUntil.toServer(senmes);// 向服务器发送信息
		qingchu();
	}
	/** 获取给予的金钱数量 */
	public static BigDecimal getqian() {
		try {
			long x = ((NumberDocument)textMoney.getDocument()).getNum().longValue();
			if (x<=0){return new BigDecimal(0);}
			if (RoleData.getRoleData().getLoginResult().getGold().longValue() < x) {return null;}
			return new BigDecimal(x);
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
	}
	//  /** 投放的方法 */
//	public static void throwinMethod() {
//		if (giveGoddstable != null && giveGoddstable.getQuality() != 1 && giveGoddstable.getQuality() != 3) {
//			if (GoodsListFromServerUntil.isExist(giveGoddstable)) {
//				return;
//			}
//			if (giveGoddstable.getUsetime() <= 0) {
//				return;
//			}
//			Goodstable goodstable = (Goodstable) giveGoddstable.clone();
//			goodstable.setUsetime(1);
//			// 发送服务器
//			String senmes = Agreement.getAgreement().throwinarticleAgreement(GsonUtil.getGsonUtil().getgson().toJson(goodstable));
//			SendMessageUntil.toServer(senmes);
//			// 从自己的物品中删除
//			int count = 1;
//			if (count > giveGoddstable.getUsetime())count = giveGoddstable.getUsetime();
//			giveGoddstable.setUsetime(giveGoddstable.getUsetime() - count);
//			GoodsMouslisten.gooduse(giveGoddstable, 1);
//			if (giveGoddstable.getUsetime() <= 0) {
//				GoodsListFromServerUntil.Delete(gerNUmber);
//			}
//		}
//	}
	@Override
	public void upNum() {
		BigDecimal j1 = ((NumberDocument)textNumber.getDocument()).getNum();
		upMoney(j1.intValue());
	}
	@Override
	public boolean isChange() {
		return false;
	}
	public goodbtn[] getBtnrights() {
		return btnrights;
	}
	public void setBtnrights(goodbtn[] btnrights) {
		this.btnrights = btnrights;
	}
}
